//
//  UILabel+STRExtension.m
//  陶瓷
//
//  Created by 王戈 on 15/10/10.
//  Copyright © 2015年 王戈. All rights reserved.
//

#import "UILabel+STRExtension.h"

@implementation UILabel (STRExtension)

- (void)suitableFontWithMaxFontNum:(NSUInteger)fontNum {
    
    NSUInteger temFontNum = fontNum;
    
    // 根据传入的字号算出占字面积大小 ,如果占字面积大于label自身面积 ,就让字号减1
    while ([self areaWithSize:[self.text sizeWithfont:[UIFont systemFontOfSize:temFontNum] maxSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)]] > [self areaWithSize:self.size]) {
        temFontNum--;
    }
    self.font = [UIFont systemFontOfSize:temFontNum];
}

// 根据size返回面积
- (CGFloat)areaWithSize:(CGSize)labelSize {
    return labelSize.width * labelSize.height;
}

@end
